﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;
using System.Diagnostics;
using System.Web.Security;
using System.ServiceModel.Syndication;
using System.Collections.Specialized;

namespace AzureReader.BLL
{
    public static class BlobManager
    {

        static string ContentBloblPrefix = "content";
        static string SummaryBloblPrefix = "summary";

        

       

        public static CloudBlobContainer StoreFeedInfo(string feedUrl, string feedTitle, string feedDescription)
        {
            //create or update container meta data
            var blobContainerName = FormsAuthentication.HashPasswordForStoringInConfigFile(feedUrl, "md5").ToLower();
            var account = CloudStorageAccount.FromConfigurationSetting("DataConnectionString");
            var blobs = account.CreateCloudBlobClient();
            CloudBlobContainer feedContainer = blobs.GetContainerReference(blobContainerName);

            feedContainer.CreateIfNotExist();
            feedContainer.Metadata["FeedUrl"] = feedUrl;
            //if (!string.IsNullOrEmpty(feedTitle))
            //    feedContainer.Metadata["Title"] = feedTitle;
            //if (!string.IsNullOrEmpty(feedDescription))
            //    feedContainer.Metadata["Description"] = feedDescription;
            feedContainer.SetMetadata();
            return feedContainer;
        }

        //public static string PrepareKeyFieldValue(string val)
        //{
        //    return val.Replace("/", "").Replace("\\", "").Replace("#", "").Replace("?", "");
        //}

        public static string StoreFeedItemContentBlob(CloudBlobContainer feedContainer, string feedItemID,string itemContent)
        {
           
            var blob = feedContainer.GetBlobReference( ContentBloblPrefix+ feedItemID.Replace("-",""));

            blob.Properties.ContentType = "text/html";

          
            
            blob.UploadText(itemContent);


            return blob.Uri.ToString();
        }
        public static string StoreFeedItemSummaryBlob(CloudBlobContainer feedContainer, string feedItemId,string itemSummary)
        {

            var blob = feedContainer.GetBlobReference(SummaryBloblPrefix + feedItemId.Replace("-",""));

            blob.Properties.ContentType = "text/html";
             
             blob.UploadText(itemSummary );


            return blob.Uri.ToString();
        }
        public static void UpdateFeedItemContentBlob(CloudBlobContainer feedContainer, string feedContentBlobUri, string content)
        {
           
            CloudBlob feeContenBlob = feedContainer.GetBlobReference( ContentBloblPrefix+feedContentBlobUri);
            feeContenBlob.UploadText(content);
        
        }
        public static void UpdateFeedItemSummaryBlob(CloudBlobContainer feedContainer, string feedSummaryBlobUri, string summary)
        {

            CloudBlob feedSummaryBlob = feedContainer.GetBlobReference(SummaryBloblPrefix + feedSummaryBlobUri);
            feedSummaryBlob.UploadText(summary);

        }
        
    }
}
